package com.wuchao.controller;


import com.alibaba.fastjson.JSON;
import com.wuchao.entity.Menu;
import com.wuchao.response.Result;
import com.wuchao.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wuchao
 * @since 2022-03-29
 */
@CrossOrigin
@Api(tags = "菜单树")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuService menuService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation(value = "加载菜单树",notes = "所有菜单树")
    @GetMapping("/tree")
    public Result tree(){//缓存加载菜单树
        //先定义一个键
        String key="menuKey";
        List<Menu> menus=null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        if(!redisTemplate.hasKey(key)){
            //List<Menu> menus=menuService.list();
             menus=menuService.findMenuTree();
            ops.set(key,menus);
        }else{
            menus= JSON.parseObject(JSON.toJSONString(ops.get(key)), ArrayList.class);
        }

        //流派思想拿openid 流派思想不能用在json中，先注释掉
    /*QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
    menuQueryWrapper.eq("open",1);//高级查询，记住很牛逼，这里可以不用，因为流派拿id*/
      /*  List<Integer> ids = menus.stream()
                .map(xjj -> xjj.getOpen()).collect(Collectors.toList());*///map为重构集合
        return Result.ok().data("tree",menus);//.data("open",ids);
    }
    @ApiOperation(value = "发送验证码",notes = "所有菜单树")
    @GetMapping("/send")
public  String sendCode(){
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set("code",
                new Random().nextInt(900000)+100000+"",15, TimeUnit.SECONDS);
        return "发送验证码成功，请注意查收";
    }

    @ApiOperation(value = "接收验证码",notes = "所有菜单树")
    @GetMapping("/getCode")
    public  Result getCode(){
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        String code=ops.get("code").toString();
        if(code==null){
            return  Result.error().message("验证码已经过期");
        }else{
            return Result.ok().data("code",code);
        }
    }

/*@ApiOperation(value = "加载菜单树",notes = "所有菜单树")
@GetMapping("/tree")
    public Result tree(){
    //List<Menu> menus=menuService.list();
    List<Menu> menus=menuService.findMenuTree();
    //流派思想拿openid
    *//*QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
    menuQueryWrapper.eq("open",1);//高级查询，记住很牛逼，这里可以不用，因为流派拿id*//*
    List<Integer> ids = menus.stream()
            .map(xjj -> xjj.getOpen()).collect(Collectors.toList());//map为重构集合
    return Result.ok().data("tree",menus).data("open",ids);
}*/
}

